package com.java2007.hotel.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.dao.FoodDao;
import com.java2007.hotel.dao.impl.FoodDaoImpl;
import com.java2007.hotel.entity.PageBean;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.service.FoodService;
import com.java2007.hotel.utils.JdbcUtils;
import jdk.nashorn.internal.scripts.JD;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/23
 **/
public class FoodServiceImpl implements FoodService {
    private FoodDao foodDao = new FoodDaoImpl();

    /**
     * 通过foodName查询
     *
     * @param foodName
     * @return
     */
    @Override
    public List<Food> findByFoodName(String foodName) throws SQLException {
        if (foodName.isEmpty()) {
            foodName = "";
        }
        try {
            List<Food> foodList = foodDao.findByFoodName("%" + foodName.trim() + "%");
            return foodList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过FoodId删除
     *
     * @param foodId
     * @return
     */
    @Override
    public int deleteByFoodId(Integer foodId) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            foodDao.deleteByFoodId(foodId);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 通过foodId查询
     *
     * @param foodId
     * @return
     */
    @Override
    public Food findByfoodId(Integer foodId) throws SQLException {
        try {
            Food food = foodDao.findByfoodId(foodId);
            return food;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 增加菜
     *
     * @param food
     * @return
     */
    @Override
    public int addFood(Food food) throws SQLException {
        try {
            JdbcUtils.startTransactions();
            foodDao.addFood(food);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 修改food信息
     *
     * @param food
     * @return
     */
    @Override
    public int updateFood(Food food) {
        try {
            JdbcUtils.startTransactions();
            foodDao.updateFood(food);
            JdbcUtils.commit();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            JdbcUtils.rollBack();
            return 0;
        }
    }

    /**
     * 分页查询菜的信息
     *
     * @param pageNo   页数
     * @param pageSize 每页显示的数量
     * @param food
     * @return
     */
    @Override
    public PageBean<Food> findFoodToPage(Integer pageNo, Integer pageSize, Food food) throws SQLException {
        PageBean<Food> pageBean = new PageBean<>();
        //判断页数是否合法
        if (pageNo <= 0) {
            pageNo = 1;
        }

        //存储参数
        List<Object> params = new ArrayList<>();

        //StringBuffer sql = new StringBuffer("SELECT food_id foodId,food_name foodName,food_type_id foodTypeId,food_price foodPrice,food_vip_price foodVipPrice,food_image foodImage,food_desc foodDesc,food_create_time foodCreateTime,food_update_time foodUpdateTime,food_creat_user_id foodCreatUserId FROM t_food WHERE 1 = 1 ");
        StringBuffer sql = new StringBuffer("SELECT food_id foodId,food_name foodName,f.food_type_id foodTypeId,t.type_name foodTypeName,food_price foodPrice,food_vip_price foodVipPrice,food_image foodImage,food_desc foodDesc,food_create_time foodCreateTime,food_update_time foodUpdateTime,food_creat_user_id foodCreatUserId FROM t_food f LEFT JOIN t_food_type t ON  f.food_type_id = t.type_id WHERE 1=1 ");
        StringBuffer sql2 = new StringBuffer("SELECT COUNT(1) FROM t_food WHERE 1 = 1");

        //判断菜类别是否存在
        if (null != food.getFoodTypeId()) {
            //判断是不是没选菜类别，全部菜列表的菜类别为0;
            if (food.getFoodTypeId() != 0) {
                sql.append(" AND food_type_id = ?");
                sql2.append(" AND food_type_id = ?");
                params.add(food.getFoodTypeId());
            }
        }
        //判断菜名是否存在
        if (null != food.getFoodName()) {
            sql.append(" AND food_name LIKE ?");
            sql2.append(" AND food_name LIKE ?");
            params.add("%" + food.getFoodName() + "%");
        }
        //查询符合条件的总数量
        Long totalCount = foodDao.findCount(sql2.toString(), params);

        //添加分页条件
        sql.append(" LIMIT ?,?");
        params.add((pageNo - 1) * pageSize);
        params.add(pageSize);

        //获取分页后的数据
        List<Food> pageList = foodDao.findFoodToPage(sql.toString(), params);

        //封装进pageBean
        pageBean.setPageList(pageList);
        pageBean.setTotalCount(totalCount);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalPage();
        pageBean.setPageNo(pageNo);
        return pageBean;
    }

    /**
     * 按照菜名查询
     * @param foodName
     * @return
     */
    @Override
    public Food findFood(String foodName) throws SQLException {
        if (foodName == null || StringUtils.isEmpty(foodName)){
            return null;
        }
        return foodDao.findFood(foodName);
    }


}
